package sesi.ejava.control;

import sesi.ejava.bcam.R;
import sesi.ejava.settings.BaseSettings;
import sesi.ejava.stream.ControlOutputStream;
import sesi.ejava.stream.ControlOutputStream.ControlData;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.ToggleButton;

public class LightsActivity extends Activity {

	private LinearLayout layoutAB ; 
	private boolean antiB ; // présence anti B à l'avant
	
	private boolean      positionLights;
	private boolean      crossingLights;
	private boolean      roadLights    ;
	private boolean      frontFogLights;
	private boolean      rearFogLights ;
	private boolean      warning       ;
	
	private ToggleButton togglePosition;
	private ToggleButton toggleCrossing;
	private ToggleButton toggleRoad    ;
	private ToggleButton toggleFrontFog;
	private ToggleButton toggleRearFog ;
	private ToggleButton toggleWarning ;
	
	private ControlOutputStream             stream   ;
	private ControlOutputStream.ControlData sendData ;
	
	@Override
	public void onCreate (Bundle savedInstanceState){
		super.onCreate(savedInstanceState) ;
		setContentView(R.layout.lights_layout) ;
		

		// récupere l'instance su flux de sortie
		stream = ControlOutputStream.getInstance() ;
		//sendData = stream.new ControlData() ;
		sendData = ControlDataSingleton.getInstance().getControlData() ;
		
		// controle présence de feux antiB à l'avant 
		antiB = BaseSettings.getsettingsSingleton(this).getAntiB() ;
		if (antiB == false) {// si non : masquer le layout concerné
			layoutAB = (LinearLayout) findViewById(R.id.linearLayoutFrontFogLights);
			layoutAB.setVisibility(View.GONE) ;
		}
		
		togglePosition = (ToggleButton) findViewById (R.id.toggle_position );
		toggleCrossing = (ToggleButton) findViewById (R.id.toggle_crossing );
		toggleRoad     = (ToggleButton) findViewById (R.id.toggle_road     );
		toggleFrontFog = (ToggleButton) findViewById (R.id.toggle_front_fog);
		toggleRearFog  = (ToggleButton) findViewById (R.id.toggle_rear_fog );
		toggleWarning  = (ToggleButton) findViewById (R.id.toggle_warning  );
		
		//LISTENERS SUR LES BOUTONS
		togglePosition.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				onTogglePositionClicked() ;
			}
		});
		toggleCrossing.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				onToggleCrossingClicked () ;
			}
		});
		toggleRoad.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				onToggleRoadClicked() ;
			}
		});
		toggleFrontFog.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				onToggleFrontFogClicked() ;
			}
		});
		toggleRearFog.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				onToggleRearFogClicked() ;
			}
		});
		toggleWarning.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				onToggleWarningClicked() ;
			}
		});
	}
	
	private void onTogglePositionClicked () {
		if (positionLights == true) { // veilleuses alumées auparavant , on les eteint
			//tout éteindre
			positionLights = false ;
			crossingLights = false ;
			roadLights     = false ;
			frontFogLights = false ;
			rearFogLights  = false ;
			// on place les indicateurs des boutons en conséquence
			togglePosition.setChecked(false);
			toggleCrossing.setChecked(false);
			toggleRoad.setChecked(false);
			toggleFrontFog.setChecked(false);
			toggleRearFog.setChecked(false);
		} else {
			positionLights = true ;
		}
		sendCommand (sendData);
	}
	private void onToggleCrossingClicked () {
		if ((crossingLights == false)){ //&& (positionLights == true)) {
			crossingLights = true ;
			toggleCrossing.setChecked(true) ;
			positionLights = true ;
			togglePosition.setChecked(true);
		} else {
			crossingLights = false ;
			roadLights     = false ;
			frontFogLights = false ;
			rearFogLights  = false ;
			toggleCrossing.setChecked(false);
			toggleRoad.setChecked    (false);
			toggleFrontFog.setChecked(false);
			toggleRearFog.setChecked (false);
		}
		sendCommand (sendData);
	}
	private void onToggleRoadClicked () {
		if ((roadLights == false) && (crossingLights == true)) {
				roadLights = true ;
				toggleRoad.setChecked(true);
		} else {
			roadLights = false ;
			toggleRoad.setChecked(false) ;
		}
		sendCommand (sendData);
	}
	private void onToggleFrontFogClicked () {
		if ((frontFogLights == false) && (positionLights == true)) {
			frontFogLights = true ;
			toggleFrontFog.setChecked(true) ;
		} else {
			frontFogLights = false ;
			toggleFrontFog.setChecked(false) ;
		}
		sendCommand (sendData);
	}
	private void onToggleRearFogClicked () {
		if ((rearFogLights == false) && (crossingLights == true)) {
			rearFogLights = true ;
			toggleRearFog.setChecked(true) ;
		} else {
			rearFogLights = false ;
			toggleRearFog.setChecked(false) ;
		}
		sendCommand (sendData);
	}
	private void onToggleWarningClicked () {
		if (warning == true) {
			warning = false ;
		} else {
			warning = true ;
		}
		sendCommand (sendData);
	}
	
	private void sendCommand (ControlData data) {
		data.setPositionLights  (positionLights);
		data.setDippedHeadlights(crossingLights);
		data.setMainBeam        (roadLights    );
		data.setRearFogLamp     (rearFogLights );
		if (antiB == true) {
			data.setFrontFogLamp(frontFogLights);
		} else {
			data.setFrontFogLamp(false) ;
		}
		data.setWarning(warning) ;
		stream.sendData(data) ;
	}
	
	protected void onStart() {
		super.onStart();
		
		positionLights = sendData.isPositionLights();
		crossingLights = sendData.isDippedHeadlights();
		roadLights     = sendData.isMainBeam();
		frontFogLights = sendData.isFrontFogLamp();
		rearFogLights  = sendData.isRearFogLamp();
		warning        = sendData.isWarning();
		
		togglePosition.setChecked(positionLights);
		toggleCrossing.setChecked(crossingLights);
		toggleRoad.setChecked    (roadLights    );
		toggleFrontFog.setChecked(frontFogLights);
		toggleRearFog.setChecked (rearFogLights );
		toggleWarning.setChecked (warning       );
	}
}





























